package com.godpaper.game.chess.twoHitOne
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.controls.Button;

	public class Piece extends Button implements IPiece
	{
		public static const COMPUTER:int = -1;
		public static const HUMAN:int = 1;
		public static const ANOTHER_HUMAN:int = 2;
		
		public static const LEFT:String = "left";
		public static const UP:String = "up";
		public static const RIGHT:String = "right";
		public static const DOWN:String = "down";
		
		public static const OFFSET_X:Number = 25;
		public static const OFFSET_Y:Number = 25;
		
		public function Piece()
		{
			//TODO: implement function
			super();
			super.width = 50;
			super.height = 50;
			super.toggle = true;
			super.addEventListener(MouseEvent.CLICK,onMouseClick);
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			(parentApplication as Rudiment).reboundOtherChessMen(this);
		}
		
		public function doMove(terminalPostion:Point):String
		{
			//TODO: implement function
			trace("super(x= "+super.x+",y= "+super.y+")");
			trace("terminalPostion(x= "+terminalPostion.x+",y= "+terminalPostion.y+")");
			var direction:String = getChessManDirectionOfMovement(new Point(x,y),terminalPostion);
			
			super.x = terminalPostion.x;
			super.y = terminalPostion.y;
			
			return direction;
		}
		
		public function undoMove(originalPostion:Point):String
		{
			//TODO: implement function
			return "";
		}
		
		protected function getChessManDirectionOfMovement(lastTimePoint:Point,thisTimePoint:Point):String
		{
			var direction:String;
			if( Math.abs( lastTimePoint.x - thisTimePoint.x )<= OFFSET_X )
			{
				direction = (lastTimePoint.y > thisTimePoint.y) ? UP:DOWN;
			}
			if( Math.abs( lastTimePoint.y - thisTimePoint.y )<= OFFSET_Y )	
			{
				direction = (lastTimePoint.x > thisTimePoint.x) ? LEFT:RIGHT;
			}
			return direction;
		}
		
	}
}